package com.zpark.servlet_context;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackageName: com.zpark.servlet_context
 * @ClassName: ServletContextDemo04
 * @Description:
 * @author: RZS
 * @date: 2023/3/8  9:27
 */
@WebServlet("/sc04.do")
public class ServletContextDemo04 extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        /**
         * 在当前servlet中绑定数据
         * 1、获取context对象
         * 2、绑定数据
         */
        ServletContext context = getServletContext();
        // 绑定数据  setAttribute(String  绑定名字， Object  绑定对象（值）)
        context.setAttribute("username", "这是demo04绑定的数据");

        User user = new User();
        user.setUsername("李四");
        user.setPassword("000000");
        user.setAge(100);
        // 绑定数据
        context.setAttribute("user", user);

        context.setAttribute("array", new int[]{
                12, 56, 89, 100
        });

        context.setAttribute("request", req);

    }
}
